package com.syezon.wifikey.api;

import com.syezon.android.base.utils.DeviceUtils;
import com.syezon.wifikey.BuildConfig;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2018/9/13.
 */

public class ApiClient {
    private static Retrofit mInstance;
    private static Retrofit mWangyiInstance;

    private static Retrofit getHttpClient() {
        if (mInstance == null) {
            if (mInstance == null) {
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                builder.readTimeout(10, TimeUnit.SECONDS);
                builder.writeTimeout(10, TimeUnit.SECONDS);
                builder.connectTimeout(5, TimeUnit.SECONDS);
                if (BuildConfig.DEBUG) {
                    // Log信息拦截器
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    builder.addInterceptor(loggingInterceptor);
                }
                OkHttpClient okHttpClient = builder.build();
                mInstance = new Retrofit.Builder()
                        .baseUrl(ApiService.BASE_URL)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .client(okHttpClient)
                        .build();
            }
        }
        return mInstance;
    }

    private static Retrofit getWangyiNewsHttpClient() {
        if (mWangyiInstance == null) {
            if (mWangyiInstance == null) {
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                builder.readTimeout(10, TimeUnit.SECONDS);
                builder.writeTimeout(10, TimeUnit.SECONDS);
                builder.connectTimeout(5, TimeUnit.SECONDS);
                if (BuildConfig.DEBUG) {
                    // Log信息拦截器
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    builder.addInterceptor(loggingInterceptor);
                }
                OkHttpClient okHttpClient = builder.build();
                mWangyiInstance = new Retrofit.Builder()
                        .baseUrl(ApiService.NEWS_BASE_URL)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .client(okHttpClient)
                        .build();
            }
        }
        return mWangyiInstance;
    }

    public static ApiService getApiService() {
        return getHttpClient().create(ApiService.class);
    }

    public static ApiService getWangYiApiService() {
        return getWangyiNewsHttpClient().create(ApiService.class);
    }

    public static Observable<String> queryYiyouNewsList(int num, int group, String channelTag) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("deviceId", DeviceUtils.getAndroidID());
        //用户平台，1：Android，2：iOS，3：H5
        paramMap.put("platform",1);
        paramMap.put("num",num);
        paramMap.put("group",group);
        paramMap.put("channelTag",channelTag);
        return ApiClient.getWangYiApiService().queryYiyouNewsList(paramMap);
    }

    public static Observable<String> getSelfNewsTab() {
        return ApiClient.getWangYiApiService().queryYiyouNewsTab(1);
    }
}
